package gagnatech.database;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import gagnatech.entity.Category;
import gagnatech.entity.Media;
import gagnatech.entity.User;

public class DatabaseDAO {

	private Database database;

	public DatabaseDAO() {
		database = Database.getInstance();
	}

	public DatabaseDAO(Database database) {
		super();
		this.database = database;
	}

	public boolean insert(Object entity) {

		try {
			database.getEntityManager().getTransaction().begin();
			database.getEntityManager().persist(entity);
			database.getEntityManager().getTransaction().commit();
		
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public Media update(Media media) {
		database.getEntityManager().getTransaction().begin();
		media = database.getEntityManager().merge(media);
		database.getEntityManager().getTransaction().commit();
		return media;
	}

	public void deleteMedia(Media media) {
		database.getEntityManager().getTransaction().begin();
		media = database.getEntityManager().merge(media);
		database.getEntityManager().remove(media);
		database.getEntityManager().getTransaction().commit();
	}

	public void insertMedia(Media media) {

		List<Media> listMedias = getAllMedia();

		
		Media media2;

		
		media2 = listMedias.get(listMedias.size()-1);
		

		media.setIdAuthor(media2.getIdAuthor());
		media.setIdCategory(media2.getIdCategory());
		media.setIdGenre(media2.getIdGenre());
		insert(media);
		
	}

	public Object select(String tableName, int id) {
		
			return database.getEntityManager().createQuery(
		            "select t from " + tableName + " t where t.id = " + id).getSingleResult();
	}

	public List<Media> getAllMedia() {	
		try {
			List<Media> listMedias = (List<Media>)database.getEntityManager().createQuery("select m from Media m").getResultList();
			return listMedias;	
		} catch (Exception e) {
			return null;
		}
	}

	public List<Media> getMediaByIdCategory(int index) {

		try {
			List<Media> listMedias = (List<Media>)database.getEntityManager().createQuery("select m from Media m Where m.category.idCategory = "+ index).getResultList();
			return listMedias;	
		} catch (Exception e) {
			return null;
		}
	}

	public List<Category> getAllCategory() {	
		try {
			List<Category> listCategory = (List<Category>)database.getEntityManager().createQuery("select m from Category m").getResultList();
			return listCategory;	
		} catch (Exception e) {
			return null;
		}
	}
	
	public Object select(String tableName, String condition) {
	
		Query query = database.getEntityManager().createQuery("SELECT " + tableName + "Value FROM " + tableName + " " + tableName + "Value WHERE " + condition);
		return query.getSingleResult();
	}

	public User getUser(String login, String mdp) {

		return (User)database.getEntityManager().createQuery(
	            "select t from User t where t.login = '" + login + "' AND t.mdp = '" + mdp + "'").getSingleResult();
	}

}













	